﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Module;
using System.Xml;
using System.Text;
using Cit.CDE.Net.Service.ModuleFactory;
using Cit.CDE.Net.Service;
using Cit.CDE.Net.Module.Commons;
using Cit.CDE.Net.Module;
using cde.SystemCommon;
using System.Web.UI.MobileControls;
using System.Collections.Generic;


/// <summary>
/// Copyright (C)   jinggubang
/// FileName:        top.aspx.cs         
/// Author :         Wangbc
/// CDT:             2011-04-01           
/// Version:         1.0.0.0                
/// Depiction:       顶部菜单    
/// </summary>
public partial class top : UserControl.MenuPage
{
    /// <summary>
    /// 初始化
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string path = Request.ApplicationPath;
            if (path == "/")
            {
                path = "";
            }

            //是否登录
            LoginOprator op = (LoginOprator)Session[Cit.CDE.Net.Module.Commons.CDEConstants.SESSION_LOIN_OPRATOR];

            if (op == null)
                return;

            user.Text = op.NAME;

            //权限XML文件
            XmlNode AllNode = (XmlNode)Session[UserControl.MenuPage.SESSION_MENU];

            //权限错误
            if (AllNode.ChildNodes.Count <= 0)
            {
                StringBuilder alertStr = new StringBuilder();
                alertStr.AppendFormat("<script language=\"javascript\">{0}", Environment.NewLine);
                alertStr.AppendFormat("alert(\"");
                alertStr.AppendFormat("没有权限，请重新登录");
                alertStr.AppendFormat("\");window.top.location='" + path + "/againLogin.aspx';</script>{0}", Environment.NewLine);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Startup", alertStr.ToString());
                return;
            }

            StringBuilder sb = new StringBuilder();
         
            //sb.Append("<li id=menuid_t1 class=\"top_banner_li_on\"><a href=\"main/course/CourseIndex.aspx\" target=\"mainFrame\"><span>首页</span></a></li>");
            //sb.Append("<li id=menuid_t2 class=\"top_banner_li_off\"><a href=\"main/course/LoreIndex.aspx\" target=\"mainFrame\"><span>知识库</span></a></li>");
            //顶部菜单初始化
            for (int i = 0; i < AllNode.ChildNodes.Count; i++)
            {
                XmlNode node = AllNode.ChildNodes[i];
                sb.Append("<li id=menuid_");
                sb.Append(i);
                if (i == 0)
                {
                   sb.Append(" class=\"top_banner_li_on\" ");
                }
                sb.Append(" >");
                sb.Append("<a href=\"javascript:changeMenu('").Append(i).Append("','").Append(AllNode.ChildNodes.Count).Append("','").Append(node.Attributes["id"].Value).Append("')");
                sb.Append("\"><span>");
                sb.Append(node.Attributes["name"].Value).Append("</span></a>");
                sb.AppendLine("</li>");
            }


           // sb.AppendLine("</ul>");

            topMenu.InnerHtml = sb.ToString();
            //Timer1_Tick(null, null);
            //左侧菜单
            StringBuilder sbr = new StringBuilder();
            sbr.AppendFormat("<script language=\"javascript\">{0}", Environment.NewLine);
            sbr.Append("top.mainFrame.location.href='main/course/CourseIndex.aspx?menuType=").Append(AllNode.ChildNodes[0].Attributes["id"].Value).Append("&shopFlag=0';");
            //sbr.AppendLine("document.getElementById(\"menuid_0\").background=\"images/menu_bg01.jpg\";");
            sbr.AppendFormat("</script>{0}", Environment.NewLine);
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "Startup", sbr.ToString());
        }
        else 
        {
            LoginOprator op = (LoginOprator)Session[Cit.CDE.Net.Module.Commons.CDEConstants.SESSION_LOIN_OPRATOR];

            if (this.user.Text != op.NAME)
            {
                string path = Request.ApplicationPath;

                if (path == "/")
                {
                    path = "";
                }
                StringBuilder ss = new StringBuilder();
                ss.AppendFormat("<script language=\"javascript\">{0}", Environment.NewLine);
                ss.AppendFormat("alert(\"");
                ss.AppendFormat("新用户已登录！");
                ss.AppendFormat("\");window.top.location='" + path + "/login.aspx';</script>{0}", Environment.NewLine);
              //  ScriptManager.RegisterStartupScript(Up_DP, Up_DP.GetType(), "Msg", ss.ToString(), false); 

                return;
                //Response.Redirect(path + "/againLogin.aspx");//   
                //return;
            }
        }
    }
    
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        //string Num = "0";
        //LoginOprator op = (LoginOprator)Session[Cit.CDE.Net.Module.Commons.CDEConstants.SESSION_LOIN_OPRATOR];

        //if (op == null)
        //    return;

        //CDEEntityManager em = CDEEMFactory.getEMByFullName(WEBConstants.EM_RECEIVINGMESSAGES);
        //CDESearchCriteria sc = new CDESearchCriteria(em);
        //sc.addCondition("ADDRESSEE", op.SysID);
        //sc.addCondition("STATE", "0");

        //Num = CDEService.LoadCount(em, sc);

        //if (Num != "0")
        //{
        //    this.MessageNum.Text = "(" + Num + ")";
        //}
        //else
        //{
        //    this.MessageNum.Text = "";
        //}
    }
}
